<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>SX</title>
  <link rel="icon" href="img/logo.png">
  <link rel="stylesheet" href="css/font-awesome.min.css">
  <link rel="stylesheet" href="css/jquery.orgchart.css">
  <link rel="stylesheet" href="css/style.css">
  <style type="text/css">
    #chart-container { height: 600px; border: 2px solid #aaa; }
    .orgchart { background: #fff; }
    .orgchart .scope-self .title { background-color: grey; }
    .orgchart .scope-all .title { background-color: red; }
  </style>
</head>
<body>
  <div id="chart-container"></div>

  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="js/jquery.orgchart.js"></script>
  <script type="text/javascript">
    $(function() {

    var datascource = {
      'name': '张三', 'title': '总经理', 'className': 'scope-all', 'isAuditor': true,
      'children': [
        { 'name': '李四', 'title': '关键客户主管',
          'children': [{ 'name': '王五', 'title': '关键客户专员' }]
        },
        { 'name': '赵六', 'title': '销售总监', 'className':'auditor', 'isAuditor': true,
          'children': [
            { 'name': '钱七', 'title': '零售经理' },
            { 'name': '孙八', 'title': '客户总监', 'className': 'scope-self',
              'children': [
                { 'name': '李逵', 'title': '客户关爱专员' },
                { 'name': '周羽', 'title': '客户经理' }
              ]
            }
          ]
        },
        { 'name': '赵云', 'title': '客服总监',
          'children': [{
            'name': '小明', 'title': '客服专员'
          }]
        }
      ]
    };
    var oc = $('#chart-container').orgchart({
      'data' : datascource,
      'nodeContent': 'title',
      'draggable': true,
      'dropCriteria': function($draggedNode, $dragZone, $dropZone) {
        if($draggedNode.find('.content').text().indexOf('manager') > -1 && $dropZone.find('.content').text().indexOf('engineer') > -1) {
          return false;
        }
        return true;
      },
      'createNode': function($node, data) {
        $node.on('click', function(event) {
          if (!$(event.target).is('.edge, .toggleBtn')) {
            var $this = $(this);
            var $chart = $this.closest('.orgchart');
            var newX = window.parseInt(($chart.outerWidth(true)/2) - ($this.offset().left - $chart.offset().left) - ($this.outerWidth(true)/2));
            var newY = window.parseInt(($chart.outerHeight(true)/2) - ($this.offset().top - $chart.offset().top) - ($this.outerHeight(true)/2));
            $chart.css('transform', 'matrix(1, 0, 0, 1, ' + newX + ', ' + newY + ')');
            alert("Hello world!");
          }
        });
        if (null != data.isAuditor && data.isAuditor) {
          $node[0].children[0].append("（审）");
        }
      }
    });

    oc.$chart.on('nodedrop.orgchart', function(event, extraParams) {
      console.log('draggedNode:' + extraParams.draggedNode.children('.title').text()
        + ', dragZone:' + extraParams.dragZone.children('.title').text()
        + ', dropZone:' + extraParams.dropZone.children('.title').text()
      );
    });

  });
  </script>
  </body>
</html>